package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.baoyz.widget.PullRefreshLayout;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.ReservationB;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.ui.me.adapter.ReservationListAdapter;
import com.cn.aam.checaiduo.utils.WindowsUtils;
import com.cn.aam.checaiduo.widget.MultiStateView;

import java.util.List;

import butterknife.BindView;

/**
 * monker on 2017/6/13.
 * 预约记录列表
 */

public class ReservationListActivity extends BaseActivity {

    @BindView(R.id.id_recyclerview)
    RecyclerView idRecyclerview;
    @BindView(R.id.id_multistateview)
    MultiStateView idMultistateview;
    @BindView(R.id.id_swipe)
    PullRefreshLayout idSwipe;

    private ReservationListAdapter adapter;

    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, ReservationListActivity.class);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_reservation_list;
    }

    @Override
    protected void initView() {
        ToolBarManager.with(this, WMContent).setTitle("预约记录")
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setTitleColor(R.color.textColor)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back);


        idSwipe.setEnabled(false);
        adapter = new ReservationListAdapter(mContext);
        idRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        int pad = WindowsUtils.dip2px(10);
        idRecyclerview.setPadding(pad, pad, pad, pad);
        idRecyclerview.setAdapter(adapter);
        getAutoTenderList();
    }

    public void getAutoTenderList() {
        RetrofitHome.getInstance().getInternalApi().getAutoTenderList(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<List<ReservationB>>>toMain())
                .subscribe(new OnRequestListener<NormalResponseB<List<ReservationB>>>(this) {
                    @Override
                    public void onNext(NormalResponseB<List<ReservationB>> value) {
                        super.onNext(value);
                        if (value.getIsLogged() != Const.STATE_LOGIN) {
                            goLogin();
                            return;
                        }
                        adapter.setDataToAdapter(value.getData());
                        idMultistateview.setViewState(value.getData().size() == 0 ? MultiStateView.VIEW_STATE_EMPTY : MultiStateView.VIEW_STATE_CONTENT);
                    }
                });
    }
}
